package cloud.service.imp;

import cloud.dao.OrdersDao;
import cloud.pojo.Orders;
import cloud.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;
import util.VagueUtil;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

@Service
@Transactional
public class OrdersServiceimp implements OrdersService {
    @Autowired
    VagueUtil<Orders> vagueUtil;

    @Autowired
    IdWorker idWorker;

    @Autowired
    OrdersDao ordersDao;

    @Override
    public void save(Orders orders) {

        ordersDao.save(orders);
    }

    @Override
    public void updatetopage(String id) {
        ordersDao.getOne(id);
    }

    @Override
    public void updates(Orders orders) {
        ordersDao.saveAndFlush(orders);
    }

    @Override
    public Page<Orders> findAll(Integer pagesize, Integer pagenumber, Orders orders) throws Exception {
        HashMap<String,Object> hm=new HashMap<>();
        Method[] sourceMethods = orders.getClass().getMethods();
        for(int i=0;i<sourceMethods.length;i++){
            if(sourceMethods[i].getName().startsWith("get")){
                String lsName = sourceMethods[i].getName().substring(3);   // 属性
                Object loValue = sourceMethods[i].invoke(orders, null);  // 值
                if(!lsName.toLowerCase().equals("class")){
                    hm.put(lsName.toLowerCase(),loValue);
                }
            }
        }
        Specification<Orders> specification = vagueUtil.createSpecification(hm);
        PageRequest pageRequest =  PageRequest.of(pagenumber-1, pagesize);
        return ordersDao.findAll(specification,pageRequest);
    }

    @Override
    public void deletes(String id) {
        ordersDao.deleteById(id);
    }

    @Override
    public HashMap<String,String> shoppurc(HashSet<String> shopid,String userid) {
        HashMap<String,Object> hm=new HashMap<>();
        HashSet<String> userids=new HashSet<>();
        userids.add(userid);
        hm.put("shopid",shopid);
        hm.put("userid",userids);
        Specification<Orders> specification = vagueUtil.createSpecification(hm);

        List<Orders> lists= ordersDao.findAll(specification);
        HashMap<String,String> h=new HashMap<>();
        for(int i=0;i<lists.size();i++){
            if(lists.get(i).getOrderpayment().equals("1")){
                h.put(lists.get(i).getShopid(),"false");
            }else{
                h.put(lists.get(i).getShopid(),"true");
            }
        }
        return h;
    }

    public List<Orders> aa() {
        HashSet<String> s=new HashSet<>();
        s.add("98162161955195");
        s.add("13213513513216");
        HashMap<String,Object> hm=new HashMap<>();
        hm.put("id",s);
        Specification<Orders> specification = vagueUtil.createSpecification(hm);
        return ordersDao.findAll(specification);
    }
}
